package com.example.service.impl;

import com.example.entity.po.AddressPO;
import com.example.entity.po.AddressPOExample;
import com.example.entity.vo.AddressVO;
import com.example.entity.vo.OrderProjectVO;
import com.example.mapper.AddressPOMapper;
import com.example.mapper.OrderProjectPOMapper;
import com.example.mapper.ProjectPOMapper;
import com.example.service.api.OrderProjectVOService;
import com.example.util.ResultEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class OrderProjectVOServiceimpl implements OrderProjectVOService {
    @Autowired
    OrderProjectPOMapper orderProjectPOMapper;
    @Autowired
    AddressPOMapper addressPOMapper;
    @Override
    public ResultEntity<OrderProjectVO> getOrderProjectVO(Integer returnId) {
        try {
            OrderProjectVO orderProjectVO = orderProjectPOMapper.selectOrderProjectVO(returnId);
            return ResultEntity.successWithData(orderProjectVO);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.failed(e.getMessage());
        }
    }
    @Override
    public ResultEntity<List<AddressVO>> getAddressListVOByMemberId(Integer memberId) {
        try {

        AddressPOExample example = new AddressPOExample();
        example.createCriteria().andMemberIdEqualTo(memberId);
        List<AddressPO> addressPOList = addressPOMapper.selectByExample(example);

        List<AddressVO> addressVOList = new ArrayList<>();
        for (AddressPO addressPO : addressPOList) {
            AddressVO addressVO = new AddressVO();
            BeanUtils.copyProperties(addressPO,addressVO);
            addressVOList.add(addressVO);
        }

        return ResultEntity.successWithData(addressVOList);}
        catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.failed(e.getMessage());
        }
    }
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    @Override
    public ResultEntity saveAddressPO(AddressVO addressVO) {

        try {
            AddressPO addressPO = new AddressPO();
            BeanUtils.copyProperties(addressVO,addressPO);
            addressPOMapper.insert(addressPO);
            return ResultEntity.successWithoutData();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.failed(e.getMessage());
        }
    }
}
